import { defineStore } from 'pinia'
// import { remove } from 'lodash-es'

export const useGoodsStore = defineStore('goods', {
	persist: {
	    enabled: true,
		detached: true, // 设置订阅与组件分离
	},
    state: () => {
		return {
			list: [],
			editTmp: null, // 临时被编辑信息的汉服
		}
    },
    actions: {
		addGoods(goods){
			this.list.unshift(goods)
		},
		deleteGoods(id){
			// const data = remove(this.list, (item)=>item.timestamp!==id)
			let arr = []
			for(let item of this.list){
				if(item.timestamp===id){
					if(item.imgUrl){
						// 如果图片存在就删除图片
						uni.removeSavedFile({
							filePath: item.imgUrl,
							complete: function (res) {
								console.log(1111, res)
							},
						});
					}
				}else{
					arr.push(item)
				}
			}
			this.list = arr
		},
		editGoods(goods){
			const index = this.list.findIndex(item=>item.timestamp===goods.timestamp)
			if(index>-1){
				this.list[index] = goods
			}
		},
		modifyGoodsState(id, num){
			const index = this.list.findIndex(item=>item.timestamp===id)
			if(index>-1){
				// num = 1 为已售完  0 或者 不存在 为正常状态
				this.list[index].state = num
			}
		},
		setEditTmp(goods){
			this.editTmp = goods
		}
    }
})